package com.guigu.servicebusiness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicebusiness.entity.TbActivity;
import com.guigu.servicebusiness.entity.TbAd;
import com.guigu.servicebusiness.mapper.TbActivityMapper;
import com.guigu.servicebusiness.service.ITbActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-09
 */
@Service
public class TbActivityServiceImpl extends ServiceImpl<TbActivityMapper, TbActivity> implements ITbActivityService {

    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbActivity> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbActivity> pageAlbum = new Page<>(page,size);
        baseMapper.selectPage(pageAlbum,wrapper);
        if (pageAlbum.getRecords() == null && pageAlbum.getTotal() == 0){
            return Result.error();
        }
        return Result.ok().data("albumRows",pageAlbum.getRecords()).data("total",pageAlbum.getTotal());
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbActivity> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbActivity> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("startTime")!=null && !"".equals(searchMap.get("startTime")))
                wrapper.ge("start_time",searchMap.get("startTime"));
            if(searchMap.get("endTime")!=null && !"".equals(searchMap.get("endTime")))
                wrapper.le("end_time",searchMap.get("endTime"));

        }
        return wrapper;
    }
}
